package suanfa

func GetCha(list []int) (cha int, newList []int, ok bool) {
	chaMap := map[int]int{}
	for i := 1; i < len(list); i++ {
		cha = list[i] - list[i-1]
		chaMap[cha] = 0
		newList = append(newList, cha)
	}
	if len(chaMap) == 1 {
		ok = true
	} else {
		ok = false
	}
	return
}

func GetShang(list []int) (shang int, newList []int, ok bool) {
	shangMap := map[int]int{}
	for i := 1; i < len(list); i++ {
		if list[i-1] == 0 {
			ok = false
			newList = []int{1, 0}
			return
		}
		shang = list[i] / list[i-1]
		if shang * list[i-1] != list[i]{
			ok = false
			newList = []int{1, 0}
			return
		}

		shangMap[shang] = 0
		newList = append(newList, shang)
	}
	if len(shangMap) == 1 {
		ok = true
	} else {
		ok = false
	}
	return
}

func CheckList(list []int) (val int, ok bool) {
	if len(list) <= 2 {
		return 0, false
	}
	cha, newList, o := GetCha(list)
	//fmt.Println(list, o)
	if o == true {
		val = list[len(list)-1] + cha
		list = append(list, val)
		ok = true
		//fmt.Println(val, ok)
		return val, ok
	}

	val, ok = CheckList(newList)
	val = val + list[len(list)-1]

	return val, ok
}
